package com.hzj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzj.mapper.ScheduleMapper;
import com.hzj.pojo.Schedule;
import com.hzj.service.ScheduleService;
import com.hzj.utils.ExcelUtil;
import com.hzj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Aisling
 * @date 2021-05-24 17:08
 */
@RestController
@RequestMapping("/provider/admin/hosp/schedule")
public class ScheduleExcelController {
    @Autowired
    ScheduleService ss;
    @Autowired
    ScheduleMapper sm;

    //创建excel 表头
    public static String[] excelTitle() {
        String[] strArray = { "编号", "医院编号", "科室编号", "职称" ,"医生名称" ,"擅长技能" ,"安排日期" ,"安排时间" ,"可预约数" ,"剩余预约数" ,"挂号费" ,"排班状态" ,"创建时间" ,"更新时间" ,"逻辑删除"};
        return strArray;
    }

    @PostMapping("/downExcel")
    public String downExcel(){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.ne("docname","");
        List<Schedule> list = sm.selectList(wrapper);
        System.out.println("list:"+list);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
        Map<String,List<String>> map=new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            ArrayList<String> schedules=new ArrayList<>();
            schedules.add(String.valueOf(list.get(i).getId()));
            schedules.add(list.get(i).getHoscode());
            schedules.add(list.get(i).getDepcode());
            schedules.add(list.get(i).getTitle());
            schedules.add(list.get(i).getDocname());
            schedules.add(list.get(i).getSkill());
            schedules.add(sdf.format(list.get(i).getWorkDate()));
            schedules.add(list.get(i).getWorkTime()+"");
            schedules.add(String.valueOf(list.get(i).getReservedNumber()));
            schedules.add(String.valueOf(list.get(i).getAvailableNumber()));
            schedules.add(String.valueOf(list.get(i).getAmount()));
            schedules.add(list.get(i).getStatus()+"");
            schedules.add(sdf.format(list.get(i).getCreateTime()));
            schedules.add(sdf.format(list.get(i).getUpdateTime()));
            schedules.add(list.get(i).getIsDeleted()+"");
            map.put(String.valueOf(i+1),schedules);
        }
        String[] excelTitles = ScheduleExcelController.excelTitle();
        String filePath = ExcelUtil.createExcel(map, excelTitles);
        return filePath;
    }
}
